import React from 'react';
import PropTypes from 'prop-types';
import {Card, Button, Row, Col, Table, Spin} from 'antd';
import {TABLE, DATE_FORMAT, DATETIME_FORMAT} from '../constants';
import {conventEnumValueToString, formatDateTime, formatPercent} from '../utils';
import {technologyActivityApprovedResult} from '../Enum';
import {hasPermissions} from './utils';
import {PERMISSION} from './constants';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import WrappedPopconfirm from '../common/WrappedPopconfirm';
import {submitData, abandonData} from './api';
import styles from './style.css';
import routes from './routes';
import {injectIntl, formatMessage} from './intl';
const {Description} = DescriptionList;

export class DetailPanel extends React.PureComponent {
    state = {
        loading: false,
    };
    componentDidMount() {
        this.props.init();
    }
    submitConfirm = () => {
        this.setState({loading: true});
        const rowVersion = this.props.data.rowVersion;
        submitData(this.props.data.id, rowVersion)
            .then(res => {
                this.setState({loading: false});
                if(res.ok)
                    this.props.successReturn();
            });
    }
    abandonConfirm = () => {
        this.setState({loading: true});
        const rowVersion = this.props.data.rowVersion;
        abandonData(this.props.data.id, rowVersion)
            .then(res => {
                this.setState({loading: false});
                if(res.ok)
                    this.props.successReturn();
            });
    }
    render() {
        const {id, data = {}, permissions, history, onCancel} = this.props;
        const operations = [];
        if(hasPermissions(permissions, PERMISSION.update) && hasPermissions(data.options, PERMISSION.update))
            operations.push(<Button key="update" onClick={() => history.push(routes.update.format(id))} size="small">{
                formatMessage({
                    id: 'detailPanel.btn.edit',
                    defaultMessage: '编辑'
                })
            }</Button>);
        if(hasPermissions(permissions, PERMISSION.submit) && hasPermissions(data.options, PERMISSION.submit))
            operations.push(
                <WrappedPopconfirm
                    id="submit"
                    key="submit"
                    title={formatMessage({
                        id: 'detailPanel.popconfirm.message',
                        defaultMessage: '是否继续?'
                    })}
                    onConfirm={this.submitConfirm}>
                    <Button key="submit" size="small">{
                        formatMessage({
                            id: 'detailPanel.btn.submit',
                            defaultMessage: '提交'
                        })
                    }</Button>
                </WrappedPopconfirm>);
        if(hasPermissions(this.props.permissions, PERMISSION.approve) && hasPermissions(data.options, PERMISSION.approve))
            operations.push(<Button key="approve" onClick={() => history.push(routes.approve.format(id))} size="small">{
                formatMessage({
                    id: 'detailPanel.btn.approve',
                    defaultMessage: '审核'
                })
            }</Button>);
        if(hasPermissions(permissions, PERMISSION.abandon) && hasPermissions(data.options, PERMISSION.abandon))
            operations.push(
                <WrappedPopconfirm
                    id="abandon"
                    key="abandon"
                    title={formatMessage({
                        id: 'detailPanel.popconfirm.message',
                        defaultMessage: '是否继续?'
                    })}
                    onConfirm={this.abandonConfirm}>
                    <Button key="abandon" size="small">{
                        formatMessage({
                            id: 'detailPanel.btn.abandon',
                            defaultMessage: '作废'
                        })
                    }</Button>
                </WrappedPopconfirm>);
        const approveColumns = [{
            title: formatMessage({
                id: 'detailPanel.column.approvedResult',
                defaultMessage: '审核结果'
            }),
            dataIndex: 'approvedResult',
            render: text => conventEnumValueToString(technologyActivityApprovedResult, text)
        }, {
            title: formatMessage({
                id: 'detailPanel.column.approveComment',
                defaultMessage: '审核意见'
            }),
            dataIndex: 'approveComment',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.approverName',
                defaultMessage: '审核人姓名'
            }),
            dataIndex: 'approverName',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.approveTime',
                defaultMessage: '审核时间'
            }),
            dataIndex: 'approveTime',
            render: text => formatDateTime(text, DATETIME_FORMAT)
        }];
        return (
            <div className="detail">
                <Spin spinning={this.state.loading}>
                    <Card>
                        <DescriptionList size="large" title={<Row><Col span={12}>{
                            formatMessage({
                                id: 'detailPanel.card.title',
                                defaultMessage: '技术升级延期申请信息'
                            })
                        }</Col>
                        <Col className={`col-align-right ${styles.detail}`} span={12}>{operations}</Col>
                        </Row>}>
                            <Description term={formatMessage({
                                id: 'detailPanel.data.brandCode',
                                defaultMessage: '品牌',
                            })}>({data.brandCode}){data.brandName}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.data.technologyActivityCode',
                                defaultMessage: '技术升级编号',
                            })}>{data.technologyActivityCode}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.data.technologyActivityName',
                                defaultMessage: '技术升级主题',
                            })}>{data.technologyActivityName}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.data.telephone',
                                defaultMessage: '电话',
                            })}>{data.telephone}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.data.email',
                                defaultMessage: '电子邮件',
                            })}>{data.email}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.data.completionRate',
                                defaultMessage: '完成率',
                            })}>{formatPercent(data.completionRate)}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.data.delayReason',
                                defaultMessage: '活动延期原因',
                            })}>{data.delayReason}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.data.recommendedFullRate',
                                defaultMessage: '活动延迟的建议完全比率',
                            })}>{formatPercent(data.recommendedFullRate)}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.data.deadlineTime',
                                defaultMessage: '活动推迟截止时间',
                            })}>{formatDateTime(data.deadlineTime, DATETIME_FORMAT)}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.data.directorOpinion',
                                defaultMessage: '奇瑞售后服务部总监意见',
                            })}>{data.directorOpinion}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.data.creatorTime',
                                defaultMessage: '创建时间',
                            })}>{formatDateTime(data.creatorTime, DATE_FORMAT)}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.data.creatorName',
                                defaultMessage: '创建人姓名',
                            })}>{data.creatorName}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.data.submitterName',
                                defaultMessage: '提交人姓名',
                            })}>{data.submitterName}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.data.submitTime',
                                defaultMessage: '提交时间',
                            })}>{formatDateTime(data.submitTime, DATETIME_FORMAT)}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.data.approveTime',
                                defaultMessage: '审核时间',
                            })}>{formatDateTime(data.approveTime, DATETIME_FORMAT)}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.data.approveComment',
                                defaultMessage: '审核意见',
                            })}>{data.approveComment}</Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.data.approverName',
                                defaultMessage: '审核人名称',
                            })}>{data.approverName}</Description>
                        </DescriptionList>
                    </Card>
                    <Card>
                        <DescriptionList title={formatMessage({
                            id: 'detailPanel.table.approveDetails',
                            defaultMessage: '审核历史清单'
                        })}>
                            <Table
                                className="white-space-nowrap"
                                columns={approveColumns}
                                rowKey="id"
                                pagination={false}
                                dataSource={data.approveDetails}
                                {...TABLE} />
                        </DescriptionList>
                    </Card>
                    {data.attachments && data.attachments.length >= 0
                        ? <Card><DescriptionList size="large" title={formatMessage({
                            id: 'detailPanel.card.attachment',
                            defaultMessage: '附件'
                        })}>
                            {data.attachments.map(item => (<Description key={item.id}>
                                <a onClick={this.onClickDownload} key="delete" data-id={item.filePath} data-applyid={item.fileName}>
                                    {item.fileName}</a></Description>))}
                        </DescriptionList></Card>
                        : <Card><DescriptionList size="large" title={formatMessage({
                            id: 'detailPanel.content.noAttachment',
                            defaultMessage: '暂无附件'
                        })}>
                        </DescriptionList></Card>}
                </Spin>
                
                <div className="page-toolbar">
                    <Button
                        type="primary"
                        disabled={this.state.loading}
                        onClick={onCancel}>{formatMessage({
                            id: 'detailPanel.btn.return',
                            defaultMessage: '返回'
                        })}</Button>
                </div>
            </div>
        );
    }
}

DetailPanel.propTypes = {
    data: PropTypes.object.isRequired,
    init: PropTypes.func.isRequired,
    loading: PropTypes.bool.isRequired,
    permissions: PropTypes.array.isRequired,
    onCancel: PropTypes.func.isRequired,
    history: PropTypes.object,
    successReturn: PropTypes.func,
};

import {connect} from 'react-redux';
import {getDetail, tableSearch} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const getDetailData = selectorFactory(['page', 'domainData', 'detail', 'data']);
const mapStateToProps = (state, props) => ({
    id: props.id,
    loading: state.getIn(['page', 'domainData', 'detail', 'isFetching']),
    permissions: getPermissions(state),
    data: getDetailData(state),
});
const mapDispatchToProps = (dispatch, props) => ({
    init: () => dispatch(getDetail(props.id)),
    onCancel: () => {
        props.history.push(routes.query.url());
    },
    successReturn: () => {
        dispatch(tableSearch());
        props.history.push(routes.query.url());
    },
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(DetailPanel));
